package cn.edu.scut.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.ShootScoreDao;
import cn.edu.scut.sys.service.ShootScoreService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.ShootScore;
import cn.edu.scut.sys.vo.ShootScoreStatistic;

@Component("shootScoreService")
public class ShootScoreServiceImpl implements ShootScoreService{

private ShootScoreDao shootScoreDao;
	
	@Resource(name="shootScoreDao")
	public void setUserDao(ShootScoreDao shootScoreDao) {
		this.shootScoreDao = shootScoreDao;
	}

	@Override
	public DataResponse<ShootScore> list(DataRequest dataRequest, HttpServletRequest request) {
		DataResponse<ShootScore> response = new DataResponse<ShootScore>();  
        int count;//总记录数  
        int limit = dataRequest.getRows() <= 0 ? 20 : dataRequest.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = dataRequest.getPage() <= 0 ? 1 : dataRequest.getPage();//当前显示页码  
        List<ShootScore> list;  
          
        count = shootScoreDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
        
        if (null != request){
        	list = shootScoreDao.list(start, limit, dataRequest.getSord(), dataRequest.getSidx(),
        			request.getParameter("unitByNowGroupId"),
        			request.getParameter("unitByNewTeamId"),
        			request.getParameter("unitByNowClassId"),
        			request.getParameter("fromDate"),
        			request.getParameter("toDate"),
        			request.getParameter("studentName"));
        } else {
        	list = shootScoreDao.list(start, limit, dataRequest.getSord(), dataRequest.getSidx(),null,null,null,null,null,null); 
        }
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(ShootScore shootScore) {
		shootScoreDao.insert(shootScore);
		return false;
	}

	@Override
	public ShootScore get(Long id) {
		return shootScoreDao.get(id);
	}

	@Override
	public boolean update(ShootScore shootScore) {
		shootScoreDao.update(shootScore);
		return true;
	}

	@Override
	public boolean del(int id) {
		
		return shootScoreDao.del(id);
	}
	
	@Override
	public DataResponse<ShootScoreStatistic> statistic(DataRequest dataRequest,
			HttpServletRequest request) {
		DataResponse<ShootScoreStatistic> response = new DataResponse<ShootScoreStatistic>();
		response.setTotal(1);  
    	response.setPage(1); 
		if (null != request){
			
        	String unitByNowGroupId = request.getParameter("unitByNowGroupId");
			String unitByNewTeamId = request.getParameter("unitByNewTeamId");
			Map<String, List<ShootScore>> shootScoreMay = new TreeMap<String, List<ShootScore>>();
			
			if (StringUtils.isEmpty(unitByNowGroupId)){
				List<ShootScore> list = shootScoreDao.list(null, null);
				for(int i = 0; i < list.size(); i++) {
					ShootScore shootScore = list.get(i);
					String unitName = shootScore.getStudent().getUnitByNowGroup().getName();
					List <ShootScore> unitByNowGroupList;
					if (shootScoreMay.containsKey(unitName)){
						unitByNowGroupList = shootScoreMay.get(unitName);
					} else {
						unitByNowGroupList = new ArrayList<ShootScore>();
					}
					unitByNowGroupList.add(shootScore);
					shootScoreMay.put(unitName, unitByNowGroupList);
				} 
				
			} else if (StringUtils.isEmpty(unitByNewTeamId)){
				List<ShootScore> list = shootScoreDao.list(unitByNowGroupId, null);
				for(int i = 0; i < list.size(); i++) {
					ShootScore shootScore = list.get(i);
					String unitName = shootScore.getStudent().getUnitByNewTeam().getName();
					List <ShootScore> unitByNowGroupList;
					if (shootScoreMay.containsKey(unitName)){
						unitByNowGroupList = shootScoreMay.get(unitName);
					} else {
						unitByNowGroupList = new ArrayList<ShootScore>();
					}
					unitByNowGroupList.add(shootScore);
					shootScoreMay.put(unitName, unitByNowGroupList);
				} 
			} else {
				List<ShootScore> list = shootScoreDao.list(unitByNowGroupId, unitByNewTeamId);
				for(int i = 0; i < list.size(); i++) {
					ShootScore shootScore = list.get(i);
					String unitName = shootScore.getStudent().getUnitByNowClass().getName();
					List <ShootScore> unitByNowGroupList;
					if (shootScoreMay.containsKey(unitName)){
						unitByNowGroupList = shootScoreMay.get(unitName);
					} else {
						unitByNowGroupList = new ArrayList<ShootScore>();
					}
					unitByNowGroupList.add(shootScore);
					shootScoreMay.put(unitName, unitByNowGroupList);
				} 
			}
        	
			response.setRecords(shootScoreMay.size()); 
			response.setRows(setList(shootScoreMay));
        } 
          
        return response;  
	}
	
	private List<ShootScoreStatistic> setList(Map<String, List<ShootScore>> shootScoreMay){
		List <ShootScoreStatistic> statisticList = new ArrayList<ShootScoreStatistic>();
		
		Set<String> k = shootScoreMay.keySet();
		String[] keys = k.toArray(new String[0]);
		for(int i = 0; i < keys.length; i++) {
			int[] num = {0,0,0,0};
			List <ShootScore> unitByNowGroupList = shootScoreMay.get(keys[i]);
			for(int j = 0; j < unitByNowGroupList.size(); j++){
				num[unitByNowGroupList.get(j).getScore()-1]++;
			}
			ShootScoreStatistic shootScoreStatistic = new ShootScoreStatistic();
			shootScoreStatistic.setUnitName(keys[i]);
			shootScoreStatistic.setTotalNum(unitByNowGroupList.size());
			shootScoreStatistic.setExcellentNum(num[0]);
			shootScoreStatistic.setGoodNum(num[1]);
			shootScoreStatistic.setPassNum(num[2]);
			shootScoreStatistic.setFailNum(num[3]);
			statisticList.add(shootScoreStatistic);
		}
		return statisticList;
	}

	@Override
	public DataResponse<ShootScore> analyze(DataRequest dataRequest,
			HttpServletRequest request) {
		DataResponse<ShootScore> response = new DataResponse<ShootScore>();  
        int count;//总记录数  
        int limit = dataRequest.getRows() <= 0 ? 20 : dataRequest.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = dataRequest.getPage() <= 0 ? 1 : dataRequest.getPage();//当前显示页码  
        List<ShootScore> list;  
          
        count = shootScoreDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
        
        if (null != request){
        	list = shootScoreDao.analyze(start, limit, dataRequest.getSord(), dataRequest.getSidx(),
        			request.getParameter("unitByNowGroupId"),
        			request.getParameter("unitByNewTeamId"),
        			request.getParameter("unitByNowClassId"),
        			request.getParameter("fromDate"),
        			request.getParameter("toDate"),
        			request.getParameter("studentName"));
        } else {
        	list = shootScoreDao.analyze(start, limit, dataRequest.getSord(), dataRequest.getSidx(),null,null,null,null,null,null); 
        }
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;
	}
}
